package com.doge.service.admin.issue;

import com.doge.bean.base.BaseParam;
import com.doge.bean.bo.issue.IssueCreateBO;
import com.doge.bean.pojo.issue.Issue;
import com.doge.bean.pojo.issue.IssueExample;
import com.doge.bean.vo.issue.IssueListVO;
import com.doge.bean.vo.issue.IssueNoDeletedVO;
import com.doge.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: IssueServiceImpl
 * @Description:
 * @author: PuJing
 * @create: 2021-11-30-16:06
 **/
@Service
public class IssueServiceImpl implements IssueService{

    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueListVO list(BaseParam param,String question) {

        PageHelper.startPage(param.getPage(), param.getLimit());

        IssueExample example = new IssueExample();

        IssueExample.Criteria criteria = example.createCriteria();

        if (question != null && !"".equals(question)) {
            // 模糊查询要加上 "%""%"；同时做逻辑删除，判断 deleted 字段
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);

        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Issue> issueList = issueMapper.selectByExample(example);


        PageInfo info = new PageInfo(issueList);

        IssueListVO issueListVO = new IssueListVO();

        issueListVO.setPage(info.getPageNum());
        issueListVO.setLimit(info.getPageSize());
        issueListVO.setPages(info.getPages());
        issueListVO.setList(issueList);
        issueListVO.setTotal(info.getTotal());

        return issueListVO;
    }


    @Override
    public IssueNoDeletedVO create(IssueCreateBO issueCreateBO) {

        Issue issue = new Issue();

        issue.setAddTime(new Date());
        issue.setUpdateTime(new Date());
        issue.setQuestion(issueCreateBO.getQuestion());
        issue.setAnswer(issueCreateBO.getAnswer());
        issue.setDeleted(false);

        int row = issueMapper.insertSelective(issue);

        if (row == 1) {

            IssueNoDeletedVO issueNoDeletedVO = new IssueNoDeletedVO();

            issueNoDeletedVO.setAddTime(issue.getAddTime());
            issueNoDeletedVO.setUpdateTime(issue.getUpdateTime());
            issueNoDeletedVO.setAnswer(issueCreateBO.getAnswer());
            issueNoDeletedVO.setQuestion(issueCreateBO.getQuestion());
            issueNoDeletedVO.setId(issue.getId());
            return issueNoDeletedVO;

        } else {
            return null;
        }
    }

    @Override
    public Issue update(Issue issue) {

        Issue issueUpdate = new Issue();

        issueUpdate.setId(issue.getId());
        issueUpdate.setQuestion(issue.getQuestion());
        issueUpdate.setAnswer(issue.getAnswer());
        issueUpdate.setAddTime(issue.getAddTime());
        issueUpdate.setUpdateTime(new Date());
        issueUpdate.setDeleted(issue.getDeleted());

        int i = issueMapper.updateByPrimaryKeySelective(issue);

        return issueUpdate;
    }


    @Override
    public int delete(IssueNoDeletedVO issueNoDeletedVO) {

        Issue issue = new Issue();

        issue.setId(issueNoDeletedVO.getId());
        issue.setQuestion(issueNoDeletedVO.getQuestion());
        issue.setAnswer(issueNoDeletedVO.getAnswer());
        issue.setAddTime(issueNoDeletedVO.getAddTime());
        issue.setUpdateTime(new Date());
        issue.setDeleted(true);

        int row = issueMapper.updateByPrimaryKeySelective(issue);

        if (row == 1) {
            return 200;
        } else {
            return 621;
        }
    }
}
